from fastapi import APIRouter
from config.db import conn
from models.Abnormal import Abnormal as AbnormalModel
from models.AbnormalHi import AbnormalHi as AbnormalHiModel
from models.ActualControl import ActualControl as ActualControlModel
from models.Annualreport import Annualreport as AnnualreportModel
from models.BaseinfoNormal import BaseinfoNormal as BaseinfoNormalModel
from models.BaseinfoSpecial import BaseinfoSpecial as BaseinfoSpecialModel
from models.Branch import Branch as BranchModel
from models.Changeinfo import Changeinfo as ChangeinfoModel
from models.ConsumptionRestriction import ConsumptionRestriction as ConsumptionRestrictionModel
from models.Contact import Contact as ContactModel
from models.CourtAnnouncement import CourtAnnouncement as CourtAnnouncementModel
from models.CourtRegister import CourtRegister as CourtRegisterModel
from models.Dishonest import Dishonest as DishonestModel
from models.DishonestHi import DishonestHi as DishonestHiModel
from models.EndCase import EndCase as EndCaseModel
from models.EquityInfo import EquityInfo as EquityInfoModel
from models.EquityInfoHi import EquityInfoHi as EquityInfoHiModel
from models.EquityRatio import EquityRatio as EquityRatioModel
from models.GetPledgeReg import GetPledgeReg as GetPledgeRegModel
from models.Holder import Holder as HolderModel
from models.Illegalinfo import Illegalinfo as IllegalinfoModel
from models.Inverst import Inverst as InverstModel
from models.InverstHi import InverstHi as InverstHiModel
from models.Investtree import Investtree as InvesttreeModel
from models.LandPublicity import LandPublicity as LandPublicityModel
from models.LandPublicityDetail import LandPublicityDetail as LandPublicityDetailModel
from models.LandTransfer import LandTransfer as LandTransferModel
from models.LandTransferDetail import LandTransferDetail as LandTransferDetailModel
from models.LawSuit import LawSuit as LawSuitModel
from models.LawSuitDetail import LawSuitDetail as LawSuitDetailModel
from models.OwnTax import OwnTax as OwnTaxModel
from models.ParentCompany import ParentCompany as ParentCompanyModel
from models.PunishmentInfo import PunishmentInfo as PunishmentInfoModel
from models.PunishmentInfoHi import PunishmentInfoHi as PunishmentInfoHiModel
from models.PurchaseLand import PurchaseLand as PurchaseLandModel
from models.Staff import Staff as StaffModel
from models.Zhixinginfo import Zhixinginfo as ZhixinginfoModel
from models.ZhixingHi import ZhixingHi as ZhixingHiModel

from sqlalchemy import select
from sqlalchemy.orm import Session

session = Session()

abnormal = APIRouter()
abnormalHi = APIRouter()
actualControl = APIRouter()
annualreport = APIRouter()
baseinfoNormal = APIRouter()
baseinfoSpecial = APIRouter()
branch = APIRouter()
changeinfo = APIRouter()
consumptionRestriction = APIRouter()
contact = APIRouter()
courtAnnouncement = APIRouter()
courtRegister = APIRouter()
dishonest = APIRouter()
dishonestHi = APIRouter()
endCase = APIRouter()
equityInfo = APIRouter()
equityInfoHi = APIRouter()
equityRatio = APIRouter()
getPledgeReg = APIRouter()
holder = APIRouter()
illegalinfo = APIRouter()
inverst = APIRouter()
inverstHi = APIRouter()
investtree = APIRouter()
landPublicity = APIRouter()
landPublicityDetail = APIRouter()
landTransfer = APIRouter()
landTransferDetail = APIRouter()
lawSuit = APIRouter()
lawSuitDetail = APIRouter()
ownTax = APIRouter()
parentCompany = APIRouter()
punishmentInfo = APIRouter()
punishmentInfoHi = APIRouter()
purchaseLand = APIRouter()
staff = APIRouter()
zhixinginfo = APIRouter()
zhixingHi = APIRouter()

# 经营异常
@abnormal.get("/mr/abnormal")
def get_abnormal(keyword: str):
    found_ent = conn.execute(select(AbnormalModel).where(AbnormalModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "经营异常不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

# 历史经营异常
@abnormalHi.get("/hi/abnormal")
def get_abnormalHi(keyword: str):
    found_ent = conn.execute(select(AbnormalHiModel).where(AbnormalHiModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "历史经营异常不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

# 疑似实际控制人
@actualControl.get("/ic/actualControl")
def get_actualControl(keyword: str):
    found_ent = conn.execute(select(ActualControlModel).where(ActualControlModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "疑似实际控制人不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

# 企业年报
@annualreport.get("/ic/annualreport")
def get_annualreport(keyword: str):
    found_ent = conn.execute(select(AnnualreportModel).where(AnnualreportModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": " 企业年报不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

# 根据企业名称查询基本信息
@baseinfoNormal.get("/ic/baseinfo/normal")
def get_baseinfoNormal(keyword: str):
    found_ent = conn.execute(select(BaseinfoNormalModel).where(BaseinfoNormalModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "企业基本信息不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

# 根据特殊企业名称查询基本信息
@baseinfoSpecial.get("/ic/baseinfo/special")
def get_baseinfoSpecial(keyword: str):
    found_ent = conn.execute(select(BaseinfoSpecialModel).where(BaseinfoSpecialModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "特殊企业基本信息不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

# 分支机构
@branch.get("/ic/branch")
def get_branch(keyword: str):
    found_ent = conn.execute(select(BranchModel).where(BranchModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "分支机构不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

# 查询变更信息
@changeinfo.get("/ic/changeinfo")
def get_changeinfo(keyword: str):
    found_ent = conn.execute(select(ChangeinfoModel).where(ChangeinfoModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "变更信息不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

# 限制消费令
@consumptionRestriction.get("/jr/consumptionRestriction")
def get_consumptionRestriction(keyword: str):
    found_ent = conn.execute(select(ConsumptionRestrictionModel).where(ConsumptionRestrictionModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "限制消费令不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@contact.get("/ic/contact")
def get_contact(keyword: str):
    found_ent = conn.execute(select(ContactModel).where(ContactModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "企业不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@courtAnnouncement.get("/jr/courtAnnouncement")
def get_courtAnnouncement(keyword: str):
    found_ent = conn.execute(select(CourtAnnouncementModel).where(CourtAnnouncementModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": " 法院公告不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@courtRegister.get("/jr/courtRegister")
def get_courtRegister(keyword: str):
    found_ent = conn.execute(select(CourtRegisterModel).where(CourtRegisterModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "立案信息不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@dishonest.get("/jr/dishonest")
def get_dishonest(keyword: str):
    found_ent = conn.execute(select(DishonestModel).where(DishonestModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "失信人不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@dishonestHi.get("/hi/dishonest")
def get_dishonestHi(keyword: str):
    found_ent = conn.execute(select(DishonestHiModel).where(DishonestHiModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "历史失信人不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@endCase.get("/jr/endCase")
def get_endCase(keyword: str):
    found_ent = conn.execute(select(EndCaseModel).where(EndCaseModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "终本案件不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@equityInfo.get("/mr/equityInfo")
def get_equityInfo(keyword: str):
    found_ent = conn.execute(select(EquityInfoModel).where(EquityInfoModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "股权出质不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@equityInfoHi.get("/hi/equityInfo")
def get_equityInfoHi(keyword: str):
    found_ent = conn.execute(select(EquityInfoHiModel).where(EquityInfoHiModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "股权出质不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@equityRatio.get("/equityRatio")
def get_equityRatio(keyword: str):
    found_ent = conn.execute(select(EquityRatioModel).where(EquityRatioModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "股权结构图不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@getPledgeReg.get("/getPledgeReg")
def get_getPledgeReg(keyword: str):
    found_ent = conn.execute(select(GetPledgeRegModel).where(GetPledgeRegModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "知识产权出质不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@holder.get("/ic/holder")
def get_holder(keyword: str):
    found_ent = conn.execute(select(HolderModel).where(HolderModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "股东不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@illegalinfo.get("/mr/illegalinfo")
def get_illegalinfo(keyword: str):
    found_ent = conn.execute(select(IllegalinfoModel).where(IllegalinfoModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "严重违法不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@inverst.get("/ic/inverst")
def get_inverst(keyword: str):
    found_ent = conn.execute(select(InverstModel).where(InverstModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "投资信息不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@inverstHi.get("/hi/inverst")
def get_inverstHi(keyword: str):
    found_ent = conn.execute(select(InverstHiModel).where(InverstHiModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "历史投资信息不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@investtree.get("/investtree")
def get_investtree(keyword: str):
    found_ent = conn.execute(select(InvesttreeModel).where(InvesttreeModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "股权穿透图不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@landPublicity.get("/m/landPublicity")
def get_landPublicity(keyword: str):
    found_ent = conn.execute(select(LandPublicityModel).where(LandPublicityModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "地块公示s不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@landPublicityDetail.get("/m/landPublicity/detail")
def get_landPublicityDetail(keyword: str):
    found_ent = conn.execute(select(LandPublicityDetailModel).where(LandPublicityDetailModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "购地详情不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@landTransfer.get("/m/landTransfer")
def get_landTransfer(keyword: str):
    found_ent = conn.execute(select(LandTransferModel).where(LandTransferModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "土地转让不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@landTransferDetail.get("/m/landTransfer/detail")
def get_landTransferDetail(keyword: str):
    found_ent = conn.execute(select(LandTransferDetailModel).where(LandTransferDetailModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "土地转让详情不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@lawSuit.get("/jr/lawSuit")
def get_lawSuit(keyword: str):
    found_ent = conn.execute(select(LawSuitModel).where(LawSuitModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "法律诉讼不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@lawSuitDetail.get("/jr/lawSuit/detail")
def get_lawSuitDetail(keyword: str):
    found_ent = conn.execute(select(LawSuitDetailModel).where(LawSuitDetailModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "法律诉讼详情不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@ownTax.get("/mr/ownTax")
def get_ownTax(keyword: str):
    found_ent = conn.execute(select(OwnTaxModel).where(OwnTaxModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "欠税公告不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@parentCompany.get("/ic/parentCompany")
def get_parentCompany(keyword: str):
    found_ent = conn.execute(select(ParentCompanyModel).where(ParentCompanyModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "总公司不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@punishmentInfo.get("/mr/punishmentInfo")
def get_punishmentInfo(keyword: str):
    found_ent = conn.execute(select(PunishmentInfoModel).where(PunishmentInfoModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "行政处罚不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@punishmentInfoHi.get("/hi/punishmentInfo")
def get_punishmentInfoHi(keyword: str):
    found_ent = conn.execute(select(PunishmentInfoHiModel).where(PunishmentInfoHiModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "历史行政处罚不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@purchaseLand.get("/m/purchaseLand")
def get_purchaseLand(keyword: str):
    found_ent = conn.execute(select(PurchaseLandModel).where(PurchaseLandModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "购地信息不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@staff.get("/ic/staff")
def get_staff(keyword: str):
    found_ent = conn.execute(select(StaffModel).where(StaffModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "人员不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@zhixinginfo.get("/jr/zhixinginfo")
def get_zhixinginfo(keyword: str):
    found_ent = conn.execute(select(ZhixinginfoModel).where(ZhixinginfoModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "被执行人信息不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }

@zhixingHi.get("/hi/zhixing")
def get_zhixingHi(keyword: str):
    found_ent = conn.execute(select(ZhixingHiModel).where(ZhixingHiModel.NAME == keyword)).first()
    if found_ent is None:
        return {
            "result": None,
            "reason": "历史被执行人信息不存在",
            "error_code": 300000
        }
    return {
        "result": found_ent,
        "reason": "ok",
        "error_code": 0
    }